package de.schmidke.palmreader;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class AddressDat extends Readable {
	private final Logger logger = Logger.getLogger(AddressDat.class.getName());

	private int versionTag;

	public int getVersionTag() {
		return versionTag;
	}

	public String getTableString() {
		return tableString;
	}

	public int getNextFreeCatId() {
		return nextFreeCatId;
	}

	public int getCatCount() {
		return catCount;
	}

	public CategoryEntry[] getCategories() {
		return categories;
	}

	public int getResourceId() {
		return resourceId;
	}

	public int getFieldsPerRow() {
		return fieldsPerRow;
	}

	public int getRecIdPos() {
		return recIdPos;
	}

	public int getRecStatusPos() {
		return recStatusPos;
	}

	public int getPlacementPos() {
		return placementPos;
	}

	public int getFieldCount() {
		return fieldCount;
	}

	public int[] getFieldEntry() {
		return fieldEntry;
	}

	public int getNumEntries() {
		return numEntries;
	}

	public AddressEntry[] getAddressEntries() {
		return addressEntries;
	}

	private boolean newFileFormat;
	private String tableString;
	private int nextFreeCatId;
	private int catCount;
	private CategoryEntry[] categories;
	private int resourceId;
	private int fieldsPerRow;
	private int recIdPos;
	private int recStatusPos;
	private int placementPos;
	private int fieldCount;
	private int[] fieldEntry;
	private int numEntries;
	private AddressEntry[] addressEntries;
	private Map<Integer, CategoryEntry> cats;

	public AddressDat() {
	}

	@Override
	protected void load() throws IOException {
		versionTag = readInt();
		newFileFormat = (versionTag == 0xcafebabe);
		
		if (newFileFormat) {
			System.out.println("New Palm format detected");
			String intro = readString();
			logger.fine("intro: " + intro);
			readUntilFindByteWithValue(0);
			String ignore = readString();
			logger.fine("ignore: " + ignore);
			skipBytes(24);
		}
		String fileName = readString();
		logger.fine("filename: " + fileName);
		tableString = readString();
		
		if (newFileFormat) {
			skipBytes(10);
			for (int i = 0; i < 9; i++) {
				readString();
				readInt();
			}
			skipBytes(42);
		}
		
		nextFreeCatId = readInt();
		catCount = readInt();
		categories = new CategoryEntry[catCount];
		cats = new HashMap<Integer, CategoryEntry>();
		for (int i = 0; i < catCount; i++) {
			categories[i] = new CategoryEntry();
			categories[i].load(is);
			cats.put(categories[i].getCatIndex(), categories[i]);
		}
		resourceId = readInt();
		fieldsPerRow = readInt();
		recIdPos = readInt();
		recStatusPos = readInt();
		placementPos = readInt();
		fieldCount = readUnsignedShort();
		fieldEntry = new int[fieldCount];
		for (int i = 0; i < fieldCount; i++) {
			fieldEntry[i] = readUnsignedShort();
		}
		numEntries = readInt() / fieldCount;
		addressEntries = new AddressEntry[numEntries];
		for (int i = 0; i < numEntries; i++) {
			addressEntries[i] = new AddressEntry(this);
			addressEntries[i].load(is);
		}
	}

	public CategoryEntry getCat(int index) {
		return cats.get(index);
	}

	public boolean isNewFileFormat() {
		return newFileFormat;
	}
}
